<template>
	<view class="page">
		<view class="return" @click="toReturn()" style="box-shadow: 1rpx 2rpx 4rpx 2rpx gainsboro;">
			<u-icon name="arrow-left" size="20"></u-icon>
		</view>
		<!-- <u-tabs :list="routeList" @click="click" :activeStyle="activeStyle" lineColor="#00AF91"></u-tabs> -->
		<navigator hover-class="none" :url="'/pages/news/detail?id='+item.id+'&type=2'" class="list flex align"
			v-for="(item,index) in list" :key="index">
			<!-- <view class="list flex align" v-for="(item,index) in newList" :key="index"> -->
			<image :src="item.cover" mode="aspectFill"></image>
			<view class="list-right">
				<view class="tit">{{item.title}}</view>
				<!-- <view class="info">红谷滩区沙井街道打造的邻里中心让居民享受多种服</view> -->
				<view class="eye flex between">
					<view>{{item.create_time}}</view>
					<view class="flex align"><u-icon color="#017DF9" name="eye"></u-icon>{{item.pv}}</view>
				</view>
			</view>
		</navigator>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
	</view>
</template>

<script>
	import {
		articleList,
		articleChannelList
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				activeStyle: {
					color: '#00AF91'
				},
				routeList: [{
					id: 3,
					name: '全部',
				}],
				totalPage: 1,
				page: 0,
				offset: 10,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				channel_id: 3,
				ws: null,
				path: '/pages/news/news'
			}
		},
		onLoad(option) {
			if(option.name){
				window.document.title = option.name
			}
			this.getChannel()
			this.getlist()
		},
		onShow() {
			this.connectWebSocket()
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		onHide() {
			// 页面隐藏/切入后台时触发
			let that = this
			that.sendData({
				type: 'page_leave',
				path: that.path,
				params: ""
			});
		},
		onUnload() {
			// 页面卸载时触发
			let that = this
			that.sendData({
				type: 'page_leave',
				path: that.path,
				params: ""
			});
		},
		methods: {
			// 连接
			connectWebSocket() {
				let that = this
				const wsUrl = "wss://gxtravel.ncscloudmedia.com/wss"; // 确保使用wss（WebSocket Secure）如果服务器支持
				that.ws = new WebSocket(wsUrl);
				that.ws.onopen = () => {
					//console.log("连接已建立");
					// 这里可以发送初始化消息等
					that.sendData({
						type: 'page_enter',
						path: that.path,
						params: ""
					});
				};
				that.ws.onmessage = (event) => {
					console.log("Message from server ", event.data);
					// 处理接收到的消息
				};
				that.ws.onerror = (error) => {
					console.error("连接失败: ", error);
				};
				that.ws.onclose = () => {
					console.log("连接失败，重新连接");
					// 可以在这里尝试重新连接
					that.connectWebSocket();
				};
			},
			// 发送
			sendData(data) {
				if (this.ws && this.ws.readyState === WebSocket.OPEN) {
					// this.ws.send(data); // 确保发送JSON格式的数据
					this.ws.send(JSON.stringify(data)); // 确保发送JSON格式的数据
				} else {
					console.error("WebSocket is not open");
				}
			},
			click(e) {
				this.channel_id = e.id
				this.list = []
				this.page = 0
				this.getlist()
			},
			async getChannel() {
				const result = await articleChannelList({parent_id:3})
				if (result.code == 1) {
					this.routeList = this.routeList.concat(result.data)
				}
			},
			toReturn() {
				window.history.back();
				// uni.navigateBack()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					channel_id
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					channel_id: 2
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await articleList(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							console.log((888))
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},

		}
	}
</script>

<style lang="less" scoped>
	.page {
		padding: 80rpx 32rpx;
	}

	.list {
		margin: 25rpx 0;
		box-shadow: 1rpx 2rpx 10rpx 2rpx gainsboro;
		padding: 20rpx;
		border-radius: 20rpx;

		image {
			width: 154rpx;
			height: 116rpx;
			object-fit: cover;
		}

		.list-right {
			margin-left: 12rpx;
			width: 73%;

			.tit {
				color: #333333;
				font-size: 26rpx;
				height: 72rpx;
				margin-bottom: 28rpx;
				overflow: hidden; //超出隐藏
				text-overflow: ellipsis; //溢出用省略号...显示
				display: -webkit-box; //将对象作为弹性伸缩盒子模型显示。
				-webkit-box-orient: vertical; //从上到下垂直 排列子元素（设置伸缩盒子的子元素排列方式）
				-webkit-line-clamp: 2; //表示显示的行数合上面两个属性，表示显示的行数
			}

			.info {
				color: #666666;
				font-size: 22rpx;
				margin: 10rpx 0;
			}

			.eye {
				color: #C0C4CC;
				font-size: 20rpx;
			}
		}
	}
</style>